package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.*;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;

@Controller
@RequestMapping({ "/productExchange" })
public class ProductExchangeController extends BaseController
{
    private String PREFIX;
    @Autowired
    private ProductExchangeService productExchangeService;
    @Autowired
    private WechatUserService wechatUserService;

    public ProductExchangeController() {
        this.PREFIX = "/modular/recy/productExchange/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "productExchange.html";
    }
    
    @RequestMapping({ "/add" })
    public String productExchangeAdd() {
        return this.PREFIX + "productExchange_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String productExchangeUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "productExchange_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final ProductExchange productExchange = (ProductExchange)this.productExchangeService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(productExchange);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final ProductExchange productExchangeQuery) {
        String wechatIdSearch = this.getPara("wechatIdSearch");
        List<Long> wechatIdList =new ArrayList<>();
        if(StrUtil.isNotBlank(wechatIdSearch)){
            QueryWrapper<WechatUser> wechatUserQueryWrapper = new QueryWrapper<>();
            wechatUserQueryWrapper.lambda().like(WechatUser::getUserName,wechatIdSearch);
            List<WechatUser> list = this.wechatUserService.list(wechatUserQueryWrapper);
            wechatIdList = list.stream().map(WechatUser::getId).collect(Collectors.toList());
        }
        final QueryWrapper<ProductExchange> productExchangeQueryWrapper = ControllerUtil.createQuery(productExchangeQuery, this.getHttpServletRequest());
        if(wechatIdList.size()>0){
            productExchangeQueryWrapper.lambda().in(ProductExchange::getWechatId,wechatIdList);
        }else if(StrUtil.isNotBlank(wechatIdSearch)){
            productExchangeQueryWrapper.lambda().eq(ProductExchange::getWechatId,0l);
        }
        final Page<ProductExchange> page = (Page<ProductExchange>)LayuiPageFactory.defaultPage();
        final IPage<ProductExchange> page2 = (IPage<ProductExchange>)this.productExchangeService.page((IPage)page, (Wrapper)productExchangeQueryWrapper);
        final List<ProductExchangeVO> productExchangeVOList = EntityBind.bind(page2.getRecords(), ProductExchangeVO.class);
        final List<Map<String, Object>> list = productExchangeVOList.stream().map(productExchange -> BeanUtil.beanToMap(productExchange)).collect(Collectors.toList());
        page2.setRecords((List)new ProductExchangeWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final ProductExchange productExchange) {
        this.productExchangeService.save(productExchange);
        return ProductExchangeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.productExchangeService.removeById((Serializable)id);
        return ProductExchangeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final ProductExchange productExchange) {
        this.productExchangeService.updateById(productExchange);
        return ProductExchangeController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.productExchangeService.removeByIds((Collection)Arrays.asList(ids));
        return ProductExchangeController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final ProductExchange productExchange = (ProductExchange)this.productExchangeService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(productExchange);
        model.addAttribute("productExchange", new ProductExchangeWrapper(map).wrap());
        return this.PREFIX + "productExchange_show.html";
    }
}
